/**
 * Created by jun on 15/8/14.
 */
var AV = require('leanengine');
var async = require('async');
var UserM=require('../models/modUser.js');

var config = require('../config.js');
//商品有关的模型
var UserM=require('../models/modUser.js');
module.exports = {
    /*
    * 根据用户openid获取积分，余额，头像
    * */
    getPointsAmountByOpenID:function(req,res,next){
        var uid=req.AV.user.id;
        var user = {};
        UserM.getUserInfo(uid).then(function(data){
            user.points = data.get('point');
            user.amount = data.get('amount');
            user.headimg = req.AV.user.get('headimgurl');
            user.nickname = data.get('nickname');
            user.gCoin = data.get('gCoin');
            res.render('user/center',{user:user,title:'用户中心'});
        }).catch(function(err){
            console.log(err);
        })
    },
    getPointsAmountByOpenID_old:function(req,res,next){
        var openid = req.AV.user.get('openid');
        var user = new Object();

        try{
            async.parallel([
                function(callback){
                    //console.log('1');
                    UserM.getPointsByOpenID(openid).then(function(point){
                        user.points=point;
                        callback(null,point);
                    })
                },
                function(callback){
                    //console.log('1');
                    UserM.getGcoinByOpenID(openid).then(function(Gcoin){
                        user.gcoin=Gcoin;
                        console.log(user.gcoin);
                        callback(null,Gcoin);
                    })
                },
                function(callback){
                    //console.log('2');
                    UserM.getAmountByOpenID(openid).then(function(amount){
                        user.amount=amount;
                        callback(null,amount);
                    })
                },
                function(callback){
                    //console.log('3');
                    UserM.getUserHeadIMG(openid).then(function(headimg){

                        // user.headimg=headimg; //使用leancloud中的头像
                        user.headimg=req.AV.user.get('headimgurl');
                        callback(null,headimg);
                    })
                },
                function(callback){
                    //console.log('4');
                    UserM.getNicknameByOpenid(openid).then(function(nickname){
                        user.nickname=nickname;
                        callback(null,nickname);
                    })
                }
            ],function(err,result){
                if(err){
                    //TODO:
                    return;
                }
                //console.log('5');
                res.render('user/center',{user:user,title:'用户中心'});
            });

            //user.points=UserM.getPointsByOpenID(openid);
            //
            //console.log('123456789000'+UserM.getPointsByOpenID(openid));
            //
            //user.amount=UserM.getAmountByOpenID(openid);
            //console.log('123456789111'+UserM.getAmountByOpenID(openid));
            //
            //user.headimg=UserM.getUserHeadIMG(openid);
            //res.render('user/center',{user:user,title:'用户中心'});
        }catch(e){
            console.log(e);
        }
    },
    center:function(req, res, next){
        res.render("user/center",{title:'个人中心'});
    },
    member:function(req, res, next){
        res.render("user/member",{title:'我的会员'});
    },
    history:function(req, res, next){
        res.render("user/history",{title:'个人订单'});
    },
    coupon:function(req, res, next){
        res.render("user/coupon",{title:'我的优惠券'});
    },
    groupHistory:function(req, res, next){
        res.render("user/groupHistory",{title:'团购订单'});
    },
    collect:function(req, res, next){
        res.render("user/collect",{title:'我的收藏'});
    },
    balance:function(req, res, next){
        res.render("user/balance",{title:'余额消费记录'});
    },
    point:function(req, res, next){
        res.render("user/point",{title:'积分消费记录'});
    },
    gcoin:function(req, res, next){
        res.render("user/gcoin",{title:'活动余额消费记录'});
    }


};
